package com.qm.util;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import com.qm.model.Score;

public class UserExcelExport {
	public static String xlsxFile = "test.xls";

	public static XSSFWorkbook export_results(List<Score> users) {
		XSSFWorkbook wb = new XSSFWorkbook();// 产生工作薄对象
		XSSFSheet sheet = wb.createSheet();// 产生工作表的对象
		// 设置第一个工作表的名称为firstSheet
		// 为了工作表能够支持中文，设置字符编码为UTF_8

		wb.setSheetName(0, "firstSheet");
		XSSFRow row = sheet.createRow(0); // 产生一行

		XSSFCell number = row.createCell(0); // 产生第一行的第一个单元格
		number.setCellType(XSSFCell.CELL_TYPE_STRING);// 设置单元格内容为字符串型
		// 为了能在单元格中写入中文，设置字符编码为UTF-8

		number.setCellValue("学号");// 往第一个单元格中写入信息
		XSSFCell name = row.createCell(1);
		name.setCellType(XSSFCell.CELL_TYPE_STRING);
		name.setCellValue("姓名");

		XSSFCell grade = row.createCell(2);
		grade.setCellType(XSSFCell.CELL_TYPE_STRING);
		grade.setCellValue("姓名");

		XSSFCell scouseName = row.createCell(3);
		scouseName.setCellType(XSSFCell.CELL_TYPE_STRING);
		scouseName.setCellValue("课程名称");

		XSSFCell scouseCode = row.createCell(4);
		scouseCode.setCellType(XSSFCell.CELL_TYPE_STRING);
		scouseCode.setCellValue("课程代码");

		XSSFCell score = row.createCell(5);
		score.setCellType(XSSFCell.CELL_TYPE_STRING);
		score.setCellValue("成绩");

		for (int i = 1; i < users.size(); i++) {
			XSSFRow rowContent = sheet.createRow(i);
			Score student_score = users.get(i);
			for (int j = 0; j <= 6; j++) {
				XSSFCell cellContent = rowContent.createCell(j);
				switch (j) {
				case 0:
					cellContent.setCellValue(student_score.getSnumber());
					break;
				case 1:
					cellContent.setCellValue(student_score.getSname());
					break;
				case 2:
					cellContent.setCellValue(student_score.getSgrade());
					break;
				case 3:
					cellContent.setCellValue(student_score.getSscouseName());
					break;
				case 4:
					cellContent.setCellValue(student_score.getScouseCode());
					break;
				case 5:
					cellContent.setCellValue(student_score.getScore());
					break;
				default:
					break;
				}
			}

		}
		return wb;
	}

}
